package com.sky.controller.admin;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.sky.dto.CategoryDTO;
import com.sky.dto.CategoryPageQueryDTO;
import com.sky.entity.Category;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.CategoryService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Api(tags = "分类管理相关接口")
@RestController("adminCategoryController")
@RequestMapping("/admin/category")
@Slf4j
public class CategoryController {
    @Autowired
    private CategoryService categoryService;

    /**
     * 新增分类
     *
     * @param categoryDTO 分类信息
     */
    @ApiOperation(value = "新增分类")
    @PostMapping
    public Result addCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.addCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 更新分类
     *
     * @param categoryDTO 分类信息
     */
    @ApiOperation(value = "更新分类信息")
    @PutMapping
    public Result updateCategory(@RequestBody CategoryDTO categoryDTO) {
        categoryService.updateCategory(categoryDTO);
        return Result.success();
    }

    /**
     * 更新分类状态
     *
     * @param status 状态
     * @param id     分类主键
     */
    @ApiOperation(value = "更新分类状态")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "分类状态（1开启 0关闭）", required = true, example = "1", paramType = "path", dataType = "int"),
            @ApiImplicitParam(name = "id", value = "分类主键", required = true, example = "1", paramType = "query", dataType = "long")
    })
    @PostMapping("/status/{status}")
    public Result updateCategoryStatus(@PathVariable Integer status, @RequestParam Long id) {
        categoryService.updateCategoryStatus(status, id);
        return Result.success();
    }

    /**
     * 根据 ID 删除分类
     *
     * @param id 分类主键
     */
    @ApiOperation(value = "根据 ID 删除分类")
    @DeleteMapping
    public Result deleteCategory(@RequestParam Long id) {
        categoryService.deleteCategory(id);
        return Result.success();
    }

    /**
     * 根据 ID 查询分类
     *
     * @param id 分类主键
     * @return 分类信息
     */
    @ApiOperation(value = "根据 ID 查询分类")
    @ApiImplicitParam(name = "id", value = "分类主键", required = true, example = "1", paramType = "path", dataType = "long")
    @GetMapping("/{id}")
    public Result<Category> getCategoryById(@PathVariable Long id) {
        Category category = categoryService.getCategoryById(id);
        return Result.success(category);
    }

    /**
     * 分类分页查询
     * @param categoryPageQueryDTO 分类分页查询条件
     * @return 分类分页查询结果
     */
    @ApiOperation(value = "分类分页查询")
    @GetMapping("/page")
    public Result<PageResult> getCategoryPage(CategoryPageQueryDTO categoryPageQueryDTO) {
        // 开始分页
        PageHelper.startPage(categoryPageQueryDTO.getPage(), categoryPageQueryDTO.getPageSize());
        // 条件查询
        Page<Category> page = categoryService.getCategoryPage(categoryPageQueryDTO);
        // 返回分页数据
        return Result.success(new PageResult(page.getTotal(), page.getResult()));
    }

    /**
     * 根据类型查询分类
     * @param type 类型（1菜品分类 2套餐分类）
     * @return 分类列表
     */
    @ApiOperation(value = "根据类型查询分类")
    @ApiImplicitParam(name = "type", value = "类型", example = "1", paramType = "query", dataType = "int")
    @GetMapping("/list")
    public Result<List<Category>> getCategoryList(@RequestParam Integer type) {
        List<Category> categoryList = categoryService.getCategoryList(type);
        return Result.success(categoryList);
    }
}
